using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class projectile : MonoBehaviour
{
    public int face;
    public float Runspeed;
    private Rigidbody2D myRigidbody;
    public float damage;
    public int RepelTimes;
    public float RepelDistance, StiffTime;
    public float dietime;
    PlayerAttack playerattack;
    Transform ttransform;
    SkillCivilization civilization;
    void Start()
    {
        myRigidbody = GetComponent<Rigidbody2D>();
        //enemyattack= attackobject.GetComponent<EnemyAttack> ();
        //face = enemyattack.face;

        face = GetComponentInParent<Creation>().face;
        ttransform = GetComponent<Transform>();
        Debug.Log(face);
        //playerattack = GameObject.FindWithTag("Attack").GetComponent<PlayerAttack>();
        if(face<0)
            ttransform.localRotation = Quaternion.Euler(0, 0, 0);
        Debug.Log(ttransform.localRotation);
        civilization = GameObject.FindWithTag("Player").GetComponentInChildren<SkillCivilization>();
        if (civilization != null&&transform.parent.parent==null)
        {
            damage = damage * (1 + civilization.damageratio);
        }
        transform.parent = null;
    }
    IEnumerator kill()
    {
        yield return new WaitForSeconds(0.1f);
           Destroy(gameObject);
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Enemy"))
        {

            other.GetComponent<EnemyBasic>().TakeDamage(damage, face, RepelTimes, RepelDistance, StiffTime);
            StartCoroutine(kill());
        }
        if (other.gameObject.CompareTag("GrindingTower"))
        {

            other.GetComponent<Grinding>().TakeDamage(damage);
            StartCoroutine(kill());
        }
    }
    // Update is called once per frame
    void Update()
    {
        dietime -= Time.deltaTime;
        if (dietime <= 0) Destroy(gameObject);
        Vector2 vel = new Vector2(-face * Runspeed, myRigidbody.velocity.y);
        myRigidbody.velocity = vel;
    }
}
